package com.shire.shire_interface.service.marketing;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.utils.RandomGen;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.dao.marketing.WxGroupKeywordDao;
import com.shire.shire_interface.dao.marketing.WxGroupKeywordItemDao;
import com.shire.shire_interface.model.cs.WxChat;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.group.WxGroupInvite;
import com.shire.shire_interface.model.marketing.WxGroupKeyword;
import com.shire.shire_interface.model.marketing.WxGroupKeywordExample;
import com.shire.shire_interface.model.marketing.WxGroupKeywordItem;
import com.shire.shire_interface.model.marketing.WxGroupKeywordItemExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.chat.WxChatService;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.group.impl.WxGroupInviteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class WxGroupKeywordService {
    @Resource
    private WxGroupKeywordDao keywordDao;
    @Resource
    private WxGroupKeywordItemDao itemDao;

    @Resource
    private WxGroupService groupService;

    @Resource
    private WxGroupInviteService groupInviteService;

    @Resource
    private WxChatService wxChatService;

    public void checkKeywordToInvite(WxChat chat, WxAccount account, WxFriend wxFriend){
        WxGroupKeywordExample keywordExample = new WxGroupKeywordExample();
        keywordExample.createCriteria().andWxaccountEqualTo(account.getWxid()).andStatusEqualTo(Constants.CommonStatus.ENABLE.getCode());
        List<WxGroupKeyword> keywords = keywordDao.selectByExampleWithBLOBs(keywordExample);
        if (keywords == null || keywords.size()<1){
            return;
        }for (WxGroupKeyword word: keywords){
            JSONObject chatJson = JSONObject.parseObject(chat.getContent());
            String content = chatJson.getString("content");
            if (content.contains(word.getKeyword())){
                WxGroupExample groupExample = new WxGroupExample();
                groupExample.createCriteria().andWxaccountEqualTo(account.getWxid())
                        .andGroupnoIn(Arrays.asList(word.getGroupnos().split(",")));
                groupExample.setOrderByClause("personNum asc");

                List<WxGroup> groupList = groupService.selectByExample(groupExample);
                if (groupList != null && groupList.size() > 0){
                    //拉群
                    WxGroupInvite wxGroupInvite = groupInviteService.genInviteGroupCmd(account.getCsid(),  account, wxFriend, groupList.get(0));
                    if(wxGroupInvite!=null) {
                        wxChatService.genNoticeChat(wxGroupInvite.getCmdid(), RandomGen.getRandomString(20), wxFriend,
                                "发送群邀请["+groupList.get(0).getGroupname()+"]", account.getCsid());
                        WxGroupKeywordItem keywordItem = new WxGroupKeywordItem();
                        keywordItem.setAvatar(wxFriend.getAvatar());
                        keywordItem.setCmdid(wxGroupInvite.getCmdid().intValue());
                        keywordItem.setCreatetime(new Date());
                        keywordItem.setCsid(account.getCsid());
                        keywordItem.setGroupno(wxGroupInvite.getGroupno());
                        keywordItem.setWxid(wxFriend.getFriendwxid());
                        keywordItem.setJoinstatus(0);
                        keywordItem.setNickname(wxFriend.getFriendnick());
                        keywordItem.setRespid(word.getId());
                        keywordItem.setWxaccount(account.getWxid());

                        itemDao.insert(keywordItem);
                    }
                    return;
                }
            }
        }
    }

    public int inserBySelective(WxGroupKeyword wxGroupKeyword) {

        return keywordDao.insertSelective(wxGroupKeyword);
    }

    public int update(WxGroupKeyword wxGroupKeyword){
        return keywordDao.updateByPrimaryKeySelective(wxGroupKeyword);
    }

    public int updateByAfterJoin(String wxAccount, String groupNo, List<String> wxidList){
        WxGroupKeywordItemExample example = new WxGroupKeywordItemExample();
        example.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo).andWxidIn(wxidList);
        WxGroupKeywordItem keywordItem = new WxGroupKeywordItem();
        keywordItem.setJointime(new Date());
        keywordItem.setJoinstatus(1);
        return itemDao.updateByExampleSelective(keywordItem, example);
    }

    public List<WxGroupKeyword> selectByExample(WxGroupKeywordExample wxGroupKeywordExample) {
        return keywordDao.selectByExampleWithBLOBs(wxGroupKeywordExample);
    }

    public int delByPrimaryKey(Integer id) {
        return keywordDao.deleteByPrimaryKey(id);
    }
}
